﻿using Dal;
using IBll;
using IDal;
using Model;

namespace Bll
{
    /// <summary>
    /// 继承食谱接口方法
    /// </summary>
    public class HelteBll : IHeltebLL
    {

        /// <summary>
        /// 实现方法
        /// </summary>
        /// <param name="baserDa">食谱</param>
        /// <param name="baserD">分类</param>
        public HelteBll(IBaserDal<Helte> baserDa, IBaserDal<HelteType> baserD)
        {
            BaserDa = baserDa;
            BaserD = baserD;
        }

        public IBaserDal<Helte> BaserDa { get; }
        public IBaserDal<HelteType> BaserD { get; }
        /// <summary>
        /// 显示查询
        /// </summary>
        /// <param name="name">食谱标题模糊</param>
        /// <param name="helteTypeId">分类下拉框</param>
        /// <returns>通过食谱，分类下拉框查询数据</returns>
        public dynamic ShowHelte(string? name, int helteTypeId)
        {
            try//异常处理
            {

                var hlte = BaserDa.GetValues();
                var helteType = BaserD.GetValues();
                var linq = from a in hlte
                           join b in helteType
                           on a.HelteTypeId equals b.HelteTypeId
                           select new
                           {
                               a.Name,
                               a.HelteDate,
                               a.HelteNum,
                               a.HelteTypeId,
                               a.State,
                               a.HelteSc,
                               a.HeltebQ,
                                 a.HelteId,
                                 a.HelteImg,
                                  b.HelteTypeName,
                                  a.HelteName,

                           };
                if (!string.IsNullOrEmpty(name))
                {

                    linq = linq.Where(x => x.HelteName.Contains(name));
                }

                if (helteTypeId>0)
                {

                    linq = linq.Where(x => x.HelteTypeId.Equals(helteTypeId));
                }
                 return linq.ToList();

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增食谱
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> AddAnync(Helte helte)
        {
            try//异常处理
            {
                return await BaserDa.AddAnync(helte);
            }
            catch (Exception)
            {

                throw;
            }
         

        }

    
    }
}
